package hoyo.zqy.org.controller;


import hoyo.zqy.basic.util.AjaxResult;
import hoyo.zqy.basic.util.PageList;
import hoyo.zqy.org.domain.Employee;
import hoyo.zqy.org.query.EmployeeQuery;
import hoyo.zqy.org.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;


    /**
     * PathVariable("id") Long id从路径上获取id的值赋值给long id
     * @param id
     * @return
     */
    //Get-http://lockhost//employee/id
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //Get-http://lockhost//employee/
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //delete//lockhost//employee/id
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }

    /**
     * 新增和修改
     * @param employee
     * @return
     */
    //Put//lockhost//employee
    @PutMapping
    @ApiOperation(value = "新增修改" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }


    //批量删除的接口
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult deleBatch(@RequestBody Long[] ids) {
        try {
            System.out.println(Arrays.toString(ids));
            employeeService.deleBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            return new AjaxResult(false,"失败了,正在殴打程序猿中");
        }
    }

    /**
     * 店铺员工查询
     * @param shopId
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }
}
